package com.mjf.jst.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mjf.jst.domain.BcCjstSku;
import com.mjf.jst.mapper.BcCjstSkuMapper;
import com.mjf.jst.service.BcCjstSkuService;
import com.mjf.jst.service.JSTAsyncService;
import com.mjf.jst.util.DateAndTimeUtils;
import com.mjf.jst.vo.BcCjstSkuVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * desc
 * </p>
 *
 * @author chenshoumeng
 * @date 2025/7/14 15:49
 */
@Service
public class BcCjstSkuServiceImpl extends ServiceImpl<BcCjstSkuMapper, BcCjstSku> implements BcCjstSkuService {

    @Autowired
    private JSTAsyncService jstAsyncService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void asyncProduct(String start, String end) {
        List<BcCjstSkuVO.SkuDataList> vos = jstAsyncService.asyncProduct(start, end);

        List<BcCjstSku> skus = new ArrayList<>();
        for (BcCjstSkuVO.SkuDataList vo : vos) {
            BcCjstSku sku = new BcCjstSku();
            sku.setId(vo.getAutoid());
            sku.setSkuId(vo.getSku_id());
            sku.setIId(vo.getI_id());
            sku.setName(vo.getName());
            sku.setShortName(vo.getShort_name());
            sku.setSalePrice(vo.getSale_price());
            sku.setCostPrice(vo.getCost_price());
            sku.setPropertiesValue(vo.getProperties_value());
            sku.setColor(vo.getColor());
            sku.setCId(vo.getC_id());
            sku.setCategory(vo.getCategory());
            sku.setEnabled(vo.getEnabled());
            sku.setModified(DateAndTimeUtils.getDateTimeByStr(vo.getModified(), "yyyy-MM-dd HH:mm:ss"));
            sku.setSupplierId(vo.getSupplier_id());
            sku.setSupplierName(vo.getSupplier_name());
            sku.setBrand(vo.getBrand());
            sku.setMarketPrice(vo.getMarket_price());
            sku.setSkuCode(vo.getSku_code());
            sku.setSupplierSkuId(vo.getSupplier_sku_id());
            sku.setSupplierIId(vo.getSupplier_i_id());
            sku.setVcName(vo.getVc_name());
            sku.setSkuType(vo.getSku_type());
            sku.setCreator(vo.getCreator());
            sku.setCreated(DateAndTimeUtils.getDateTimeByStr(vo.getCreated(), "yyyy-MM-dd HH:mm:ss"));
            sku.setRemark(vo.getRemark());
            sku.setItemType(vo.getItem_type());
            sku.setUnit(vo.getUnit());
            sku.setAutoid(vo.getAutoid());
            skus.add(sku);
        }
        if (!skus.isEmpty()) {
            saveOrUpdateBatch(skus);
        }
    }
}
